package service;

import bean.Note;
import dao.util.NoteCreate;
import mapper.NoteMapper;

import java.util.List;

public class NoteService {
    private final NoteCreate create = new NoteCreate();

    public List<Note> listAll(Integer userId) {
        NoteMapper mapper = create.getMapperClass();
        List<Note> list = mapper.selectAllNote(userId);
        create.closeSession();
        return list;
    }

    // 分页查询（可选分类）
    public List<Note> listPaged(Integer userId, Integer categoryId, Integer offset, Integer pageSize) {
        NoteMapper mapper = create.getMapperClass();
        List<Note> list = mapper.selectPaged(userId, categoryId, offset, pageSize);
        create.closeSession();
        return list;
    }

    // 统计数量（可选分类）
    public int countByFilter(Integer userId, Integer categoryId) {
        NoteMapper mapper = create.getMapperClass();
        int c = mapper.countByFilter(userId, categoryId);
        create.closeSession();
        return c;
    }

    public Note getById(Integer id, Integer userId) {
        NoteMapper mapper = create.getMapperClass();
        Note note = mapper.selectById(id, userId);
        create.closeSession();
        return note;
    }

    public int add(Note note) {
        NoteMapper mapper = create.getMapperClass();
        int r = mapper.insertNote(note);
        create.commit();
        create.closeSession();
        return r;
    }

    public int update(Note note) {
        NoteMapper mapper = create.getMapperClass();
        int r = mapper.updateNote(note);
        create.commit();
        create.closeSession();
        return r;
    }

    public int delete(Integer id, Integer userId) {
        NoteMapper mapper = create.getMapperClass();
        int r = mapper.deleteLogic(id, userId);
        create.commit();
        create.closeSession();
        return r;
    }

    public List<Note> search(Integer userId, String keyword, Integer categoryId, Integer offset, Integer pageSize) {
        NoteMapper mapper = create.getMapperClass();
        List<Note> list = mapper.searchNotes(userId, keyword, categoryId, offset, pageSize);
        create.closeSession();
        return list;
    }

    public int countSearch(Integer userId, String keyword, Integer categoryId) {
        NoteMapper mapper = create.getMapperClass();
        int c = mapper.countBySearch(userId, keyword, categoryId);
        create.closeSession();
        return c;
    }

    public int count(Integer userId) {
        NoteMapper mapper = create.getMapperClass();
        int c = mapper.countByUserId(userId);
        create.closeSession();
        return c;
    }

    public List<Note> recent7Days(Integer userId) {
        NoteMapper mapper = create.getMapperClass();
        List<Note> list = mapper.selectRecent7Days(userId);
        create.closeSession();
        return list;
    }
}